<!DOCTYPE html>
<html lang="zh">
<head>
    #@header()
    #set(isAdd = account  == null ? true : false, isEdit = !isAdd)
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <input hidden name="id" id="accountId" value="#(account.id??)">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required is-required">用户名：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="username" id="username" value="#(account.username??)" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">密码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="password" name="password" id="password" value="#(account.password??)" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">确认密码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="password" name="repassword" id="repassword" value="#(account.password??)" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">试错次数：</label>
            <div class="col-sm-8">
                <input class="form-control" type="number" name="errorTime" id="errorTime" value="#(account.errorTime??0)" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>🔒密码试错次数大于等于4次直接锁住该账户</span>
            </div>

        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">排序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="number" name="sort" id="sort" value="#(account.sort??0)" required>
            </div>
        </div>



    </form>
</div>
#@footer()
<script type="text/javascript">

    $("#form-role-add").validate({
        rules: {
            username:{
                required:true,
                remote: {
                    url: "#(prj)/super-admin/account/usernameUnique",
                    type: "get",
                    dataType: "json",
                    data: {
                        "id": function() {
                            return $("#accountId").val();
                        },
                        "username": function() {
                            return $("input[name='username']").val();
                        }
                    },
                    dataFilter: function (data, type) {
                        var res = JSON.parse(data);
                        if(res.state == 'ok'){
                            return true
                        }else{
                            return false
                        }
                    }
                },
                maxlength:20
            },
            password: {
                required: true,
                minlength: 5
            },
            repassword: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },

            onkeyup: true,

        },
        messages: {
            "username": {
                remote: "账户已存在"
            },
        },
        focusCleanup: true
    });

    $('input').on('ifChanged', function (obj) {
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == 1) {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "2") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.setting.check.chkboxType = {"Y": "ps", "N": "ps"};
            } else {
                $._tree.setting.check.chkboxType = {"Y": "", "N": ""};
            }
        }
    })

    var uurl = "";
    #if(isAdd)
    uurl = "#(prj)/super-admin/account/save";
    #else
    uurl = "#(prj)/super-admin/account/update";
    #end

    function submitHandler() {

        if ($.validate.form()) {
            #(isAdd ? 'add()' : 'edit()')
        }
    }

    function #(isAdd ? 'add()' : 'edit()') {

        $.ajax({
            url:uurl,
            type: "post",
            dataType: "json",
            data: {
                "account.id":$("#accountId").val(),
                "account.username":$("input[name='username']").val(),
                "account.password":$("input[name='password']").val(),
                "account.sort":$("input[name='sort']").val(),
                "account.error_time":$("input[name='errorTime']").val(),
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.operate.successCallbackMsg(result.msg);
/*                $.modal.alertSuccess(result.msg);
                $.modal.closeLoading();*/
            }
        });
    }

    /*success: function (data) {
        $.operate.successCallbackMsg(data.msg);
    }*/

</script>
</body>
</html>
